package cn.jiahua.test20;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 原子性
 */
public class Test2 {

    private volatile  static AtomicInteger num=new AtomicInteger();


    public static  void add(){

           num.getAndIncrement();

    }

    public static void main(String[] args) {


        new Thread(()->{
            for (int i=0;i<50000;i++){
                add();
            }
        }).start();

        new Thread(()->{
            for (int i=0;i<50000;i++){
                add();
            }
        }).start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(num);


    }



}
